<?php

// requires Monkake object

class _site_handler {
	// Monkake
	public $M;
	// Instantiated version of this object here
	public $this;
	public $Secured = TRUE;
	public $Username = 'admin';
	public $Password = 'p@ssw0rd';

	// View accessible variables/methods
	public $BodyId;
	public $BodyClass;
	public $Section;
	public $Title = '';
	public $ControllerFile;
	public $SelfLink = '';
	public $Url;
	public $BackLink;
	public $Ajax = FALSE;
	public $ShowNav = TRUE;
	public $Content = '';
	public $NavLinks = array();

	public $SectionInfo = NULL;

	/* Initialize site */
	public function InitializeSite($m) {
		$this->M = $m;

		if ( isset($_GET['ajax']) ) $this->Ajax = TRUE;
		session_start();

		$this->Url = $this->M->Get('site_url', NULL, TRUE);

		if ( $this->Secured && !$this->CheckLogin() ) {
			$this->Redirect($this->Url . 'login');
		}

		$result = $this->Query("SELECT * FROM msnu_lade_asection WHERE enabled=1");

		while ( $row = mysql_fetch_assoc($result) ) {
			$this->NavLinks[] = new Anchor(array('Href' => $this->Url . 'list?section=' . $row['name'], 'Text' => $row['dname']));
		}
	}

	public function Redirect($url) {
		header('Location: ' . $url);
		exit;
	}

	public $DbConn = NULL;
	public function Query($query) {
		if ( !$this->DbConn ) {
			$this->DbConn = mysql_connect($this->M->Get('DB_ADDR'), $this->M->Get('DB_USER'), $this->M->Get('DB_PASS'));
			mysql_select_db($this->M->Get('DB_NAME'));
		}

		$result = mysql_query($query);
		return $result;
	}

	/* Template members/methods */
	public $WrapperFile;
	public $thisFile;
	public function SetWrapperFile($name) {
		$this->WrapperFile = $this->GetViewFile($name);
	}

	public function SetViewFile($name) {
		$this->ViewFile = $this->GetViewFile($name);
	}

	public function GetViewFile($name) {
		$this_file = $this->M->Get('view_directory', NULL, TRUE) . $name . $this->M->Get('view_append');
		if ( !file_exists($this_file) ) return FALSE;
		return $this_file;
	}

	public function RenderViewContent($name) {
		$this->SetViewFile($name);
		$template = $this->M->GetObject('Template');
		if ( !$template->SetFileName($this->ViewFile) ) $this->M->Error('Could not render template:' . $this->ViewFile, TRUE);
		$template->SetPageContent(array('page' => $this));
		if ( $this->WrapperFile ) $template->SetWrapperFileName($this->WrapperFile);
		$template->RenderOutput(array('page' => $this));
	}

	public function RenderView($file_name, $variables) {
		$template = new Template();
		$file_name = $this->GetViewFile($file_name);
		if ( !$template->SetFileName($file_name) ) $this->M->Error('Could not render template:' . $file_name);
		$template->RenderOutput($variables);
	}

	public $_lade;
	public function Lade() {
		if ( $this->_lade ) return $this->_lade;

		$this->_lade = $this->M->GetObject('Lade', $this->M->Get('DB_ADDR'), $this->M->Get('DB_USER'), $this->M->Get('DB_PASS'), $this->M->Get('DB_NAME'));

		$this->_lade->AddTables($this->M->Get('msnu_tables'));
		$this->_lade->AddSections($this->M->Get('msnu_sections'));

		return $this->_lade;
	}

	public function CheckLogin() {
		return $_SESSION['gcmsusername'];
	}
}

class Anchor {
	public $Href = '';
	public $Text = '';
	public $Current = FALSE;
	public function __construct($params) {
		if (isset($params['Href'])) $this->Href = $params['Href'];
		if (isset($params['Text'])) $this->Text = $params['Text'];
		if (isset($params['Current'])) $this->Current = $params['Current'];
	}
}